/*
 *  Robot Client
 *  Relays messages to Robot Server
 *
 *     MB Sp 13
 */

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class RobotClient {

    private static   Socket robotSocket = null;
    private static   PrintWriter out = null;
    private static   BufferedReader in = null;
    private static   BufferedReader stdIn;


    ///////////////////////////////////////////////////////////////////////
    public static String read()
    {
        String fromServer = "";
        try{
            if ((fromServer = in.readLine()) != null)
            {
                return fromServer;
            }
        }
        catch(Exception e)
        {
        }
        return null;
    }

    ///////////////////////////////////////////////////////////////////////
    public static void write( String msg )
    {
        out.println( msg );
    }

    ///////////////////////////////////////////////////////////////////////
    public static boolean connect( String arg[] )
    {
        int i = 0;
        boolean done = false;
        int port = 4444;
        String host = "localhost";
        try
        {
            host = arg[0];
        }
        catch(Exception e) { }

        try
        {
            port  = Integer.parseInt(arg[1]);
        }
        catch(Exception e) { }


        // try for 10 seconds
        while ( !done && i++ < 200 )
        {
            try {
                robotSocket = new Socket(host, port);
                out = new PrintWriter(robotSocket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(robotSocket.getInputStream()));
                done = true;
            } catch (UnknownHostException e) {
                System.err.println("Don't know about host: " + host);
                return false;
            } catch (IOException e) {
                System.err.println("Couldn't get I/O for the connection to: " + host);
                System.err.println( e.getMessage() + " I will try again in 1 second" );
                sleep( 1000);
            }
        }
        System.out.println("Connected to host " + host + " on port " + port );
        stdIn = new BufferedReader(new InputStreamReader(System.in));
        return done;

    }
    ///////////////////////////////////////////////////////////////////////
    public static boolean connect(String host, int port)
    {
        int i = 0;
        boolean done = false;

        while( !done && i++<10){
            try {
                robotSocket = new Socket(host, port);
                out = new PrintWriter(robotSocket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(robotSocket.getInputStream()));
                done = true;
            } catch (UnknownHostException e) {
                System.err.println("Don't know about host: " + host);
                return false;
            } catch (IOException e) {
                System.err.println("Couldn't get I/O for the connection to: " +
                                   host + " on port " + port);
                System.err.println( e.getMessage() + " I will try again in 1 second");
                sleep(1000);
            }
        }
        stdIn = new BufferedReader(new InputStreamReader(System.in));
        return done;
    }

    //---------------------------------------------------------
    public static void sleep( int milliseconds )
    {
        try
        {
            Thread.sleep( milliseconds );
        }
        catch ( java.lang.InterruptedException e )
        {  }
    }

    ///////////////////////////////////////////////////////////////////////
    public static boolean disconnect()
    {
        try
        {
            out.close();
            in.close();
            stdIn.close();
            robotSocket.close();

        } catch (IOException e) {
            return false;
        }

        return true;
    }



    public static void main(String s[])
    {

        connect( s );

        Scanner scan = new Scanner(System.in);

        System.out.println( "Server says: " + read() +"\n");

        while( true)
        {
            System.out.println("Enter n, e, s, w or c (scan) >>");
            String cmd = scan.next().trim().toLowerCase();
            if(cmd.startsWith("n"))
                write(RobotProtocol.north);
            else if(cmd.startsWith("e"))
                write(RobotProtocol.east);
            else if(cmd.startsWith("s"))
                write(RobotProtocol.south);
            else if(cmd.startsWith("w"))
                write(RobotProtocol.west);
            else if(cmd.startsWith("c"))
                write(RobotProtocol.scan);

            System.out.println( "Server says: " + read() +"\n");

        }




    }
}
